<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */


class MeetingtodosController extends AppController {

/**
 * Define $name
 *
 */
    var $name = 'Meetingtodos';
/**
 * Define $helpers
 *
 */
    var $helpers = array('Html', 'Form');
    
/**
 * Describe index
 *
 * @param $committee
 * @param $id
 * @return null
 */
    function index($committee,$id=null) {
        $this->Committee->recursive=-1;
        $this->Meetingtodo->unbindmodel(array('belongsTo'=>array('Meeting')),false);
        $this->Meetingtodo->Meeting->recursive=-1;
        $issecretariat=$this->Meetingtodo->Meeting->Attendance->isSecretariat($id,$this->Auth->user('id'));
        $this->set('secretariat',$issecretariat);
        if(empty($issecretariat)){
            if(empty($this->permission['Meetingtodo']['sum'])){
                $this->notpermit();
            }
        }
        if (!empty($this->data['Meetingtodo'])) {
            $semakan_prev_updated = Set::extract('/Meetingtodo/id',$this->Meetingtodo->find('first',array('conditions'=>array('meeting_id'=>$id,'done'=>'1'),'fields'=>'Meetingtodo.*')));
            $semakan_prev_notupdated = Set::extract('/Meetingtodo/id',$this->Meetingtodo->find('first',array('conditions'=>array('meeting_id'=>$id,'done'=>'0'),'fields'=>'Meetingtodo.*')));
            $semakan_now = $this->data['Meetingtodo'];
            foreach ($semakan_now as $data) {
                if ($data['done'] == '1') {
                    if (in_array($data['id'],$semakan_prev_updated)) {
                        continue;
                    } else {
                        $semakan = $this->Meetingtodo->find('first',array('conditions'=>array('Meetingtodo.id'=>$data['id'])));
                        $semakan['Meetingtodo']['date_done'] = date('Y-m-d');
                        $semakan['Meetingtodo']['done'] = $data['done'];
                        if ($this->Meetingtodo->save($semakan))
                            $this->Session->setFlash(__("The Meeting's todo has been saved", true));
                    }
                }
                if ($data['done'] == '0') {
                    if (in_array($data['id'],$semakan_prev_notupdated)) {
                        continue;
                    } else {
                        $semakan = $this->Meetingtodo->find('first',array('conditions'=>array('Meetingtodo.id'=>$data['id'])));
                        $semakan['Meetingtodo']['date_done'] = date('Y-m-d');
                        $semakan['Meetingtodo']['done'] = $data['done'];
                        if ($this->Meetingtodo->save($semakan))
                            $this->Session->setFlash(__("The Meeting's todo has been saved", true));
                    }
                }
            }
        }
        
        // for use with workflow
        $this->Meetingtodo->curUser=$this->Auth->user('id');
        
        $todos=$this->Meetingtodo->find('all',array('conditions'=>array('meeting_id'=>$id)));
        $meeting = $this->Meetingtodo->Meeting->find('first',array('conditions'=>array('Meeting.id'=>$id)));
        $this->set(compact('todos','meeting'));
    }

/**
 * Describe view
 *
 * @param $committee
 * @param $id
 * @return null
 */
    function view($committee,$id = null) {
        $this->Meetingtodo->Meeting->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        if (!$id) {
            $this->Session->setFlash(__('Invalid Meetingtodo.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('meetingsTodo', $this->Meetingtodo->read(null, $id));
        $priorities = array('1'=>__('Immediate', true), '2'=>__('Urgent', true), '3'=>__('High', true), '4'=>__('Normal', true), '5'=>__('Low', true), '6'=>__('None', true));
        $this->set('priorities',$priorities);
    }

/**
 * Describe add
 *
 * @param $committee
 * @param $id
 * @return null
 */
    function add($committee,$id) {
        $this->Meetingtodo->Meeting->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Meetingtodo->Meeting->Behaviors->detach('Comment');
        $this->Meetingtodo->Meeting->Behaviors->detach('MultiFile');
        $issecretariat=$this->Meetingtodo->Meeting->Attendance->isSecretariat($id,$this->Auth->user('id'));
        if(empty($issecretariat)){
            if(empty($this->permission['Meetingtodo']['Create'])){
                $this->notpermit();
            }
        }
        if (!empty($this->data)) {
            $this->Meetingtodo->create();
            if ($this->Meetingtodo->save($this->data)) {
                $this->Session->setFlash(__('The Meetingtodo has been saved', true));
                $this->redirect(array('committee'=>$committee,'controller'=>'meetingtodos','action'=>'index','id'=>$this->data['Meetingtodo']['meeting_id']));
            } else {
                $this->Session->setFlash(__('The Meetingtodo could not be saved. Please, try again.', true));
            }
        }        
        $meetings = $this->Meetingtodo->Meeting->find('list',array('conditions'=>array('committee_id'=>$this->dcommittee['Committee']['id']),'contain'=>'Todo'));
        $priorities = array('1'=>__('Immediate', true), '2'=>__('Urgent', true), '3'=>__('High', true), '4'=>__('Normal', true), '5'=>__('Low', true), '6'=>__('None', true));
        $this->set(compact('meetings','priorities'));
        $this->set('meetingid',$id);
        $this->set('users', $this->Meetingtodo->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']));
        $this->set('returnpage',$this->referer(null,true));
    }

/**
 * Describe edit
 *
 * @param $committee
 * @param $id
 * @return null
 */
    function edit($committee,$id = null) {
        $this->Meetingtodo->Meeting->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Meetingtodo->Meeting->Behaviors->detach('Comment');
        $this->Meetingtodo->Meeting->Behaviors->detach('MultiFile');
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Meetingtodo', true));
            $this->redirect(array('committee'=>$committee,'action'=>'index'));
        }
        
        
        if (!empty($this->data)) {
            if ($this->Meetingtodo->save($this->data)) {
                $this->Session->setFlash(__('The Meetingtodo has been saved', true));
                if(isset($this->data['Meetingtodo']['returnpage'])){
                    $this->redirect($this->data['Meetingtodo']['returnpage']);
                }
                else{
                    $toret=$this->Meetingtodo->read(null,$id);
                    $this->redirect(array('committee'=>$committee,'controller'=>'meetingtodos','action'=>'index','id'=>$toret['Meetingtodo']['meeting_id']));
                }
            } else {
                $this->Session->setFlash(__('The Meetingtodo could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Meetingtodo->read(null, $id);
        }
        //check permission
        $issecretariat=$this->Meetingtodo->Meeting->Attendance->isSecretariat($this->data['Meeting']['id'],$this->Auth->user('id'));
        if(empty($issecretariat)){
            if(empty($this->permission['Meetingtodo']['Edit'])){
                $this->notpermit();
            }
        }
        
        $priorities = array('1'=>__('Immediate', true), '2'=>__('Urgent', true), '3'=>__('High', true), '4'=>__('Normal', true), '5'=>__('Low', true), '6'=>__('None', true));
        $this->set(compact('priorities'));
        $this->set('users', $this->Meetingtodo->User->getUsersPerCommitteeList($this->dcommittee['Committee']['id']));
        $this->set('returnpage',$this->referer(null,true));
    }

/**
 * Describe delete
 *
 * @param $committee
 * @param $id
 * @return null
 */
    function delete($committee,$id = null) {
        if (!$id) {
            $this->Session->setFlash(__("Invalid id for Meeting's To-do", true));
            $this->redirect(array('committee'=>$committee,'controller'=>'meetingtodos','action'=>'index'));
        }
        $toret=$this->Meetingtodo->read(null,$id);
        //check permission
        $issecretariat=$this->Meetingtodo->Meeting->Attendance->isSecretariat($toret['Meeting']['id'],$this->Auth->user('id'));
        if(empty($issecretariat)){
            if(empty($this->permission['Meetingtodo']['Edit'])){
                $this->notpermit();
            }
        }
        $this->Meetingtodo->delete($id);
        $this->Session->setFlash(__("Meeting's To-do deleted", true));
        $this->redirect(array('committee'=>$committee,'controller'=>'meetingtodos','action'=>'index','id'=>$toret['Meetingtodo']['meeting_id']));
    }

}
?>
